<?php

namespace app\stores\helper\payment;

use app\beautiful\logic\printer\PrinterLogic;

use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\OrderGoodsModel;
use app\common\helper\payment\PaymentPlatformBaseHelper;

use app\common\logic\SmsLogic;

use app\beautiful\model\order\GoodsOrderModel;

use app\beautiful\logic\LogsLogic;

use app\beautiful\model\member\MemberCouponModel;

use app\beautiful\logic\MemberWalletLogic;

use app\common\model\ShopGmvLogsModel;

use app\beautiful\logic\PartnerLogic;

use app\beautiful\model\setting\SettingSmsModel;

//每个子类只需要专注于自己的 respond 逻辑

class PaymentGoodsOrderHelper extends PaymentPlatformBaseHelper {

    protected  $moudel = 'stores';

    protected  $orderKey = 'mo'; //给一个KEY增大吞吐量

    protected  $respondAction = 'goodsMallRespond';

    

    public function respondCallback($orderId,$payInfo,$fail){

         $order = GoodsOrderModel::find($orderId);

        if(null === $order) return;      

        if($order->is_paid == 0){

            $order->is_paid  = 1;

            $order->pay_info = $payInfo;

            $order->verify_code = getVerifyCode($orderId);

            $order->pay_type = 'weixin';

            if($order->order_type=='set_meal'){
                $order->status = 10; //订单的状态值
                $order_goods = OrderGoodsModel::where(['order_id'=>$orderId,'is_delete'=>0])->find();
                if($order_goods && $order_goods->use_type!=1){
                    $end_time = goodsEndTime($order_goods,$order);
                    $order_goods->start = $end_time->start_time;
                    $order_goods->end = $end_time->end_time;
                    $order_goods->save();
                }
            }else{
                $order->status = $order->logistics_type == 1 ? 2 : 4; //订单的状态值
                $item = GoodsOrderItemModel::where(['order_id'=>$orderId])->find();
                if($order->order_type=='flash_sale'){
                    if($item['type']==3) $order->status = 10;
                }
            }


            if($order->logistics_type == 3) $order->status = 9; //订单的状态值

            $order->pay_time = time();

            $order->edit_time = date('Y-m-d H:i:s');

            if(empty($order->mix_pay_type)) $order->weixin = $order->need_pay;

            $order->save();

            //混合支付扣除其他支付方式的余额
            $return = mix_pay_money($order,$payInfo,2);
            //不用退款则按正常支付成功流程走
            if($return['is_refund']<1){

                if(!empty($order->member_coupon_id)){ //优惠券的使用

                    if(null !== $myCoupon = MemberCouponModel::find($order->member_coupon_id)){

                        if($myCoupon->status == 0){

                            $myCoupon->status = 1;

                            $myCoupon->save();

                            LogsLogic::coupon($order->shop_id, $order->member_id, -1, 12, $order->member_coupon_id, '', 0);

                        }

                    }

                }

                $MemberWalletLogic = new MemberWalletLogic($order->shop_id, $order->member_id);

                if(!empty($order->use_integral)){ //使用积分为商城支付

                    $MemberWalletLogic->useIntegral($order->use_integral, 9);

                }

                $needPay = round(($order->total_price * 100 + $order->freight * 100 - $order->coupon_money * 100 - $order->integral_balance * 100)/100,2);

                $MemberWalletLogic->addIntegralByPayment($needPay); //更新支付返的积分


                //单店版下单支付就分红，多店版收货后订单完成才分红
                if($order->from_type == 1) {
                    //分销记录

                    $PartnerLogic = new PartnerLogic($order->shop_id);

                    //$PartnerLogic->commission($order->member_id,$needPay);

                    $PartnerLogic->commissionNew($order->member_id, $needPay, 'order', $orderId);

                }

                //地推分红
                if($order->p_mid>0){
                    //目前推广只有套餐，套餐的数量只会是1
                    promotion_money($order->team_activity_id,$orderId,1,1);
                }
                //插入日志

                $shop_gmv_logs = new ShopGmvLogsModel();

                $shop_gmv_logs->shop_id=$order->shop_id;

                $shop_gmv_logs->balance=$needPay;

                $shop_gmv_logs->save();



                //发送短信给管理员

                $sms_setting = SettingSmsModel::where([['shop_id','=',$order->shop_id]])->find();

                if(null !== $sms_setting){

                    if($sms_setting->order_notice_admin==1){

                        $sms_logic= new SmsLogic();

                        $params=[

                            'money'=>$needPay,

                        ];

                        $sms_logic->send($order->shop_id,$sms_setting->boss_mobiles,$params,'orderNoticeAdmin');

                    }

                }

                //打印机
                if($order->logistics_type == 1){
                    $type = 'kuaidi';
                }
                if($order->logistics_type == 2){
                    $type = 'self';
                }
                if($order->logistics_type == 3){
                    $type = 'city';
                }

                if(in_array($order->logistics_type,[1,2,3])){
                    //判断是否有分区打印的产品
                    $return_goods_produce = goodsProduce($orderId);
                    $prints = $return_goods_produce['prints'];
                    $produce_printer_ids = [];
                    if($prints){
                        $printer_logic = new \app\stores\logic\printer\PrinterLogic($order->shop_id);
                        $produce_printer_ids = $printer_logic->producePrint($type,$orderId,$prints);
                    }
                    //原来的打印排除已分区打印的打印机
                    $printer_logic = new PrinterLogic($order->shop_id);
                    $printer_logic->print($type,$orderId,$produce_printer_ids);

                }

            }else{
                //自动退款后通知用户

            }


        }

    }

}